/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import transervfx.Dato.Entidad.GuiaTramite;

/**
 *
 * @author meli
 */
public class GuiaTramitesDAO implements Dao<GuiaTramite,Long> {
   
    private static GuiaTramitesDAO INSTANCE = null;
    
    
     /**
     *  Crea el Gestor Auxiliar
     */
    protected GuiaTramitesDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas  multi-hilo
     * otra prueba para evitar instanciación múltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new GuiaTramitesDAO();
        }
    }
   

    /**
     * Obtiene la instancia.
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static GuiaTramitesDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }
    

    @Override
    public void persist(GuiaTramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    //donde puedo meter una ventana de exito?
    //en el crear repartidor cuando acepta
    //ahi te lo busco
    @Override    
    public void remove(GuiaTramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
            System.out.println("ESTAS POR ELIMIAR");
            entity=em.getReference(GuiaTramite.class, entity.getId());
            em.remove(entity);
            System.out.println("LO ELIMINA");
            t.commit();
        } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }
 
    
    @Override
    public void update(GuiaTramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

          em.getTransaction().begin();
          if(em.contains(entity))
          {
            em.merge(entity);
          }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
    }

    @Override
    public GuiaTramite findById(Long id) {
       EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(GuiaTramite.class, id);

        } finally {
            em.close();
        }
    }
    
    @Override
    public List<GuiaTramite> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("GuiaTramite.findAll").getResultList();

        } finally {
            em.close();
        }
    }
    
    
}
